/*
 * Copyright (C) 2010 Fred Barrie
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.util;

import java.util.Collection;

/**
 * @author fred
 */
public final class CollectionsUtil {

	/**
	 * @param <T>
	 * @param collection
	 * @param index
	 * @return the item indicated position can be null
	 */
	public static <T> T get(final Collection<T> collection, final int index) {
		if ((collection == null) || collection.isEmpty()) {
			return null;
		}

		int count = 0;
		for (final T item : collection) {
			if (count++ == index) {
				return item;
			}
		}

		return null;
	}

	/**
	 * @param <T>
	 * @param collection
	 * @return the first item in the collection can be null
	 */
	public static <T> T getFirst(final Collection<T> collection) {
		return CollectionsUtil.get(collection, 0);
	}

	private CollectionsUtil() {
		// empty
	}
}
